using System;
using System.Collections;
using System.Windows.Forms;

namespace WowAceUpdater
{
    internal class DoubleBufferedListView : ListView
    {
        protected override void OnCreateControl()
        {
            DoubleBuffered = true;

            base.OnCreateControl();
        }
    }

    internal class VirtualListView : DoubleBufferedListView
    {
        protected override void OnCreateControl()
        {
            CheckBoxes = true;
            //VirtualMode = true;

            base.OnCreateControl();
        }
        protected override void OnMouseClick(MouseEventArgs e)
        {
            ListViewItem i = GetItemAt(e.X, e.Y);

            if (i != null)
            {
                if (e.X < i.Position.X && e.X > i.Bounds.Left)
                {
//                     OnItemCheck(new ItemCheckEventArgs(i.Index,
//                         i.Checked ? CheckState.Unchecked : CheckState.Checked,
//                         i.Checked ? CheckState.Checked : CheckState.Unchecked));
                   
                    OnItemChecked(new ItemCheckedEventArgs(i));
                    RedrawItems(i.Index, i.Index, false);
                }
            }

            base.OnMouseClick(e);
        }

        protected override void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e)
        {
            e.DrawDefault = true;
            base.OnDrawColumnHeader(e);
        }

        protected override void OnDrawItem(DrawListViewItemEventArgs e)
        {
            e.DrawDefault = true;
            if (!e.Item.Checked)
            {
                e.Item.Checked = true;
                e.Item.Checked = false;
            }

            e.Item.Checked = ((ListViewItem) e.Item.Tag).Checked;

            base.OnDrawItem(e);
        }
    }


    internal class DBTableLayoutPanel : TableLayoutPanel
    {
        protected override void OnCreateControl()
        {
            DoubleBuffered = true;

            base.OnCreateControl();
        }

        
    }

    internal class ListViewColumnSorter : IComparer
    {
        #region Fields

        private int ColumnToSort;

        private readonly CaseInsensitiveComparer ObjectCompare;

        private SortOrder OrderOfSort;

        #endregion

        #region Constructors

        public ListViewColumnSorter()
        {
            // Initialize the column to '0'.
            ColumnToSort = 0;

            // Initialize the sort order to 'none'.
            OrderOfSort = SortOrder.Ascending;

            // Initialize the CaseInsensitiveComparer object.
            ObjectCompare = new CaseInsensitiveComparer(Program.ciNeutral);
        }

        #endregion

        #region Properties

        public SortOrder Order
        {
            set { OrderOfSort = value; }
            get { return OrderOfSort; }
        }

        public int SortColumn
        {
            set { ColumnToSort = value; }

            get { return ColumnToSort; }
        }

        #endregion

        #region Public Methods

        public int Compare(object x, object y)
        {
            int compareResult;

            ListViewItem.ListViewSubItem sX = ((ListViewItem) x).SubItems[ColumnToSort];
            ListViewItem.ListViewSubItem sY = ((ListViewItem) y).SubItems[ColumnToSort];

            // Compare the two items.
            Object ox = sX.Tag ?? sX.Text;
            Object oy = sY.Tag ?? sY.Text;
            compareResult = ObjectCompare.Compare(ox, oy);

            //' Calculate the correct return value based on the object 
            //' comparison.
            if (OrderOfSort == SortOrder.Ascending)
            {
                //' Ascending sort is selected, return typical result of 
                //' compare operation.
                return compareResult;
            }
            else if (OrderOfSort == SortOrder.Descending)
            {
                //' Descending sort is selected, return negative result of 
                //' compare operation.
                return (-compareResult);
            }
            else
            {
                //' Return '0' to indicate that they are equal.
                return 0;
            }
        }

        #endregion
    }
}